package com.example.jfreechart.utils;

import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import javafx.scene.chart.PieChart;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.awt.*;
import java.io.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class PDFUtil {
    public static void main(String[] args) {
        Document document = new Document();
        //设置PDF大小为A4
        document.setPageSize(PageSize.A4);

        //PDF边距 margin
        document.setMargins(50,50,50,50);

        try {
            //设置PDF生成路径
            FileOutputStream fileOutputStream= new FileOutputStream("E:\\test\\test.pdf");

            //将文档写入磁盘
            PdfWriter writer = PdfWriter.getInstance(document,fileOutputStream);

            //内容
            String content = "业务性能";


            /*FontFactoryImp ffi = new FontFactoryImp();
            ffi.registerDirectories();*/

            //定义字体
            BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            //标题
            Font titleoFontChinese =  new  Font(baseFontChinese , 20 , Font.BOLD);
            Paragraph title = new Paragraph("业务性能概览",titleoFontChinese);
            //标题居中
            title.setAlignment(Paragraph.ALIGN_CENTER);


            //正文标题
            Font fontChinese = new Font(baseFontChinese,16,Font.NORMAL);
            Paragraph contentTitle = new Paragraph("概述：",fontChinese);
            contentTitle.setAlignment(Paragraph.ALIGN_JUSTIFIED);
            //contentTitle.setFirstLineIndent(20);

            //正文
            Font font = new Font(baseFontChinese, 14, Font.NORMAL);
            Paragraph contentText = new Paragraph("这是一个段落。这是一个段落。这是一个段落。这是一个段落。这是一个段落。这是一个段落。这是一个段落。这是一个段落。这是一个段落。",font);
            contentText.setFirstLineIndent(30);

            document.open();

            //写入内容，绑定字体
            Paragraph paragraph = new Paragraph(content,fontChinese);
            paragraph.setAlignment(Paragraph.ALIGN_CENTER);

            document.add(title);
            document.add(contentTitle);
            document.add(contentText);



            PdfPTable table = new PdfPTable(4);

            //为每列设置宽度
            table.setTotalWidth(new float[]{120,80,80,80});
            //设置表格上面的空白高度
            table.setSpacingBefore(20f);
            //设置表格下面的空白高度
            table.setSpacingAfter(20f);

            //表格内容开始
            PdfPCell cell1 = new PdfPCell(new Paragraph("应用名称",font));
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell1);

            PdfPCell cell2 = new PdfPCell(new Paragraph("业务健康度",font));
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell2);

            PdfPCell cell3 = new PdfPCell(new Paragraph("网络健康度",font));
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell3);

            PdfPCell cell4 = new PdfPCell(new Paragraph("告警数",font));
            cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell4);

            PdfPCell cell5 = new PdfPCell(new Paragraph("Https",font));
            cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell5);

            PdfPCell cell6 = new PdfPCell(new Paragraph("75",font));
            cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell6);

            PdfPCell cell7 = new PdfPCell(new Paragraph("85",font));
            cell7.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell7);

            PdfPCell cell8 = new PdfPCell(new Paragraph("10",font));
            cell8.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell8);

            //表格内容结束，将表格写入对象中
            document.add(table);

            /*PdfPHeaderCell header = new PdfPHeaderCell();

            header.addElement(paragraph);

            table.addCell(header);*/

            Paragraph chartTitle = new Paragraph("图表：",fontChinese);
            document.add(chartTitle);

            //饼状图数据集
            DefaultPieDataset dataset = new DefaultPieDataset();
            dataset.setValue("良好", new Double(30));
            dataset.setValue("一般", new Double(40));
            dataset.setValue("差", new Double(30));

            JFreeChart chart = ChartFactory.createPieChart3D("applications",dataset,true,true,false);

            PiePlot3D plot=(PiePlot3D)chart.getPlot();
            //设置label字体
            plot.setLabelFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 30));
            chart.getLegend().setItemFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 25));
            plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
            plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));
            chart.setBackgroundPaint(Color.white);
            plot.setForegroundAlpha(1.0f);
            plot.setCircular(true);

            TextTitle eTitle = new TextTitle("应用");
            java.awt.Font afont = new java.awt.Font(" 黑体",java.awt.Font.CENTER_BASELINE,40);
            eTitle.setFont(afont);
            chart.setTitle(eTitle);

            FileOutputStream chartJpg = null;
            try {
                /*chartJpg = new FileOutputStream("bpas/src/main/resources/font/testChartJpg.jpg");*/
                chartJpg=new FileOutputStream("E:\\test\\testChartJpg.jpg");
                ChartUtils.writeChartAsJPEG(chartJpg,0.9f,chart,800,1000,null);
                chartJpg.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            /*Image pieImage = Image.getInstance("bpas/src/main/resources/font/testChartJpg.jpg");*/
            Image pieImage = Image.getInstance("E:\\test\\testChartJpg.jpg");
            pieImage.setAlignment(Image.ALIGN_CENTER);
            pieImage.scaleAbsolute(328, 370);
            document.add(pieImage);

            document.close();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private boolean htmlToPdf(String htmlFile) throws Exception {
        String url = new File(htmlFile).toURI().toURL().toString();
        System.out.println("url:"+url);

        OutputStream os = new FileOutputStream("E:\\test\\htmlToPdf.pdf");
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocument(url);

        BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontChinese =  new  Font(baseFontChinese , 18 , Font.NORMAL);

        ITextFontResolver fontResolver = renderer.getFontResolver();

        PieChart chart = new PieChart();

        renderer.layout();
        renderer.createPDF(os);
        os.close();
        return false;
    }
}
